<template>
    <div class="register_container">
        <div class="register_box">
            <!--注册表单区域-->
            <el-form ref="registerFormRef" :model="registerForm" :rules="registerFormRules" label-width="0px"
                class="register_form">
                <!--用户名-->
                <el-form-item prop="id">
                    <el-input v-model="registerForm.id" prefix-icon="user" placeholder="用户名/邮箱"></el-input>
                </el-form-item>
                <!--密码-->
                <el-form-item prop="passwd">
                    <el-input v-model="registerForm.passwd" prefix-icon="lock" placeholder="密码" type="password">
                    </el-input>
                </el-form-item>
                <!--姓名-->
                <el-form-item prop="name">
                    <el-input v-model="registerForm.name" prefix-icon="avatar" placeholder="姓名">
                    </el-input>
                </el-form-item>
                <!--身份证号-->
                <el-form-item prop="number">
                    <el-input v-model="registerForm.number" prefix-icon="CreditCard" placeholder="身份证号">
                    </el-input>
                </el-form-item>
                <!--手机号-->
                <el-form-item prop="phone">
                    <el-input v-model="registerForm.phone" prefix-icon="Phone" placeholder="手机号">
                    </el-input>
                </el-form-item>
                <!--问题-->
                <el-form-item prop="question">
                    <el-input v-model="registerForm.question" prefix-icon="Help" placeholder="请设置安全问题">
                    </el-input>
                </el-form-item>
                <!--答案-->
                <el-form-item prop="answer">
                    <el-input v-model="registerForm.answer" prefix-icon="Key" placeholder="请设置安全答案">
                    </el-input>
                </el-form-item>
                <!--按钮区域-->
                <el-form-item class="btns">
                    <el-button type="primary" @click="register">注册</el-button>
                    <el-button type="info" @click="resetRegisterForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            registerForm: {
                type: 0,
                phone: '',
                name: '',
                passwd: '',
                id: '',
                number: '',
                question: '',
                answer: '',
            },
            //表单的验证规则对象
            registerFormRules: {
                id: [
                    { required: true, message: "请输入用户名/邮箱", trigger: "blur" }
                ],
                passwd: [
                    { required: true, message: "请输入密码", trigger: "blur" }
                ],
                number: [
                    { required: true, message: "请输入身份证号", trigger: "blur" }
                ],
                name: [
                    { required: true, message: "请输入姓名", trigger: "blur" }
                ]
            }
        }
    },
    methods: {
        //重置表单
        resetRegisterForm() {
            this.$refs.registerFormRef.resetFields();
        },
        //表单预验证
        register() {
            this.$refs.registerFormRef.validate(async valid => {
                if (!valid) return;
                const { data: res } = await this.$http.post("regis", this.registerForm);
                if (res.error_code !== 0) {
                    return this.$message.error(res.message);
                }
                this.$message.success('注册成功');
                this.$router.push('/login');
            });
        },
    }
};
</script>

<style lang="less" scoped>
.register_container {
    background-color: #2b4b6b;
    height: 100%;
    background-image: url("../../assets/1.jpg");
    background-Repeat:'no-repeat';
}

.register_box {
    width: 450px;
    height: 450px;
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.register_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}

.btns {
    display: flex;
    justify-content: flex-end;
    float: right;
}
</style>
